package com.ruoyi.web.controller.pv;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pv.domain.PvAppointRecord;
import com.ruoyi.pv.service.IPvAppointRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预约记录Controller
 *
 * @author zzx
 * @date 2023-04-22
 */
@Api("预约记录")
@RestController
@RequestMapping("/pv/appoint_record")
public class PvAppointRecordController extends BaseController {
    @Autowired private IPvAppointRecordService pvAppointRecordService;


    /**
     * 学生预约
     */
    @Log(title = "预约记录", businessType = BusinessType.INSERT)
    @PostMapping("/do")
    @ApiOperation("学生预约")
    public AjaxResult doAppoint(@Validated @RequestBody PvAppointRecord pvAppointRecord) {
        pvAppointRecord.setStuId(getUserId());
        pvAppointRecord.setDeptId(getDeptId());
        return pvAppointRecordService.doAppoint(pvAppointRecord);
    }

    /**
     * 查询预约记录列表
     */
    @GetMapping("/my-list")//todo
    @ApiOperation("查询当前登录用户的预约记录")
    public TableDataInfo myList(PvAppointRecord pvAppointRecord) {
        pvAppointRecord.setStuId(getUserId());
        List<PvAppointRecord> list = pvAppointRecordService.selectPvAppointRecordList(pvAppointRecord);
        return getDataTable(list);
    }

    /**
     * 查询预约记录列表
     */
    @PreAuthorize("@ss.hasPermi('pv:appoint_record:list')")
    @GetMapping("/list")
    public TableDataInfo list(PvAppointRecord pvAppointRecord) {
        pvAppointRecord.setDeptId(getDeptId());
        startPage();
        List<PvAppointRecord> list = pvAppointRecordService.selectPvAppointRecordList(pvAppointRecord);
        return getDataTable(list);
    }

    /**
     * 根据预约id查询报名班中未报名的学员
     */
    @PreAuthorize("@ss.hasPermi('pv:appoint_record:query')")
    @GetMapping("/not-appoint/{id}")
    public TableDataInfo notAppoint(@PathVariable("id")Long id) {
        return getDataTable(pvAppointRecordService.selectNotAppointList(id,getDeptId()));
    }

    /**
     * 导出预约记录列表
     */
    @PreAuthorize("@ss.hasPermi('pv:appoint_record:export')")
    @Log(title = "预约记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PvAppointRecord pvAppointRecord) {
        pvAppointRecord.setDeptId(getDeptId());
        List<PvAppointRecord> list = pvAppointRecordService.selectPvAppointRecordList(pvAppointRecord);
        ExcelUtil<PvAppointRecord> util = new ExcelUtil<>(PvAppointRecord.class);
        util.exportExcel(response, list, "预约记录数据");
    }

    /**
     * 获取预约记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pvAppointRecordService.selectPvAppointRecordById(id));
    }

    /**
     * 查询预约记录时间段列表
     */
    @GetMapping(value = "/getAppoint_record_time_list/{appointId}/{id}")
    public AjaxResult getAppointRecordTimeListByAppointId(@PathVariable("appointId") Long appointId ,@PathVariable("id") Long id) {
        return success(pvAppointRecordService.getAppointRecordTimeListByAppointId(appointId,id));
    }

    /**
     * 新增预约记录
     */
    @PreAuthorize("@ss.hasPermi('pv:appoint_record:add')")
    @Log(title = "预约记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PvAppointRecord pvAppointRecord) {
        pvAppointRecord.setDeptId(getDeptId());
        pvAppointRecord.setStatus(1);
        return toAjax(pvAppointRecordService.insertPvAppointRecord(pvAppointRecord));
    }

    /**
     * 修改预约记录
     */
    @PreAuthorize("@ss.hasPermi('pv:appoint_record:edit')")
    @Log(title = "预约记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PvAppointRecord pvAppointRecord) {
        return toAjax(pvAppointRecordService.updatePvAppointRecord(pvAppointRecord));
    }

    /**
     * 删除预约记录
     */
    @ApiOperation("删除预约记录")
    @Log(title = "预约记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pvAppointRecordService.deletePvAppointRecordByIds(ids));
    }

    @PostMapping("/updateAppoint_buyuerecord")
    public AjaxResult submitSupplementaryExamination(@RequestBody Map<String, Object> data) {
        // 参数校验
        if (data == null) {
            return AjaxResult.error("请求参数不能为空");
        }

        Object pvAppointRecordIdObj = data.get("pvAppointRecordId");
        Object appointTimeIdObj = data.get("appointTimeId");
        Object appointDateStrObj = data.get("appointDate");

        if (pvAppointRecordIdObj == null || appointTimeIdObj == null || appointDateStrObj == null) {
            return AjaxResult.error("关键参数不能为空");
        }

        Integer pvAppointRecordId = (Integer) pvAppointRecordIdObj;
        Integer appointTimeId = (Integer) appointTimeIdObj;
        String appointDateStr = (String) appointDateStrObj;

        // 日期解析异常处理
        Date appointDate;
        try {
            appointDate = DateUtil.parse(appointDateStr, "yyyy-MM-dd");
        } catch (Exception e) {
            return AjaxResult.error("日期格式解析错误，请使用yyyy-MM-dd格式");
        }

        PvAppointRecord pvAppointRecord = new PvAppointRecord();
        pvAppointRecord.setAppointDate(appointDate);
        pvAppointRecord.setId(Long.valueOf(pvAppointRecordId));
        pvAppointRecord.setAppointTimeId(Long.valueOf(appointTimeId));
        pvAppointRecord.setReservedOne("是");
        return AjaxResult.success(pvAppointRecordService.updatePvAppointRecord(pvAppointRecord));
    }
}
